<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: RLIMPORTEXPORTCATEGORIES.CLASS.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

class rlImportExportCategories extends reefless
{
	/**
	* Import categories
	**/
	function import()
	{
		global $lang, $rlActions, $rlValid, $rlCache;

		$import_data = $_POST['categories'];
		if ( !empty( $import_data ) )
		{
			$addedCategoryCount = 0;
			$langs_add = false;
			$maxPos = $this -> getRow("SELECT MAX(`Position`) AS `max` FROM `". RL_DBPREFIX ."categories`");
			$maxPos = (int)$maxPos['max'];

			foreach( $import_data as $iKey => $iCat )
			{
				if ( $import_data[$iKey]['checkbox'] && !$import_data[$iKey]['exist'] )
				{
					$maxPos++;
					$adaptParent = $rlValid -> str2key( $iCat['parent']) ;
					$parent_id = (int)$this -> getOne('ID', "`Key`='{$adaptParent}'", 'categories');
					$c_key = $rlValid -> xSql( $iCat['key'] );
					$c_name = $rlValid -> xSql( $iCat['name'] );

					$sql  = "INSERT INTO `". RL_DBPREFIX ."categories` ( `Key`, `Parent_ID`, `Position`, `Path`, `Type`, `Lock`, `Modified`, `Status` ) VALUES ";
					$sql .= "( '{$c_key}', '{$parent_id}', '{$maxPos}', '{$iCat['path']}', '{$iCat['type']}', '{$iCat['lock']}', NOW(), 'active' )";

					if ( $this -> query( $sql ) )
					{
						$addedCategoryCount++;
						foreach( $GLOBALS['languages'] as $lkey => $lvalue )
						{
							$lang_keys[] = array(
								'Code' => $GLOBALS['languages'][$lkey]['Code'],
								'Module' => 'common',
								'Status' => 'active',
								'Key' => 'categories+name+'. $c_key,
								'Value' => $c_name,
							);
						}
					}
				}
			}

			if ( !empty( $lang_keys ) )
			{
				$rlActions -> insert( $lang_keys, 'lang_keys' );
			}

			// update cache
			$rlCache -> updateCategories();

			$this -> loadClass('Notice');
			$GLOBALS['rlNotice'] -> saveNotice( str_replace("[count]", $addedCategoryCount, $lang['importExportCategories_count']) );
			$this -> redirect( array( "controller" => "importExportCategories" ) );
		}
	}

	/**
	* Export categories
	**/
	function export()
	{
		global $lang;

		if ( isset( $_POST['cat_sticky'] ) )
		{
			$categoriesExport = array();
			$tmpCategories = $this -> getAll("SELECT `ID` FROM `". RL_DBPREFIX ."categories`");
			foreach( $tmpCategories as $key => $entry )
			{
				array_push( $categoriesExport, $entry['ID'] );
			}
			unset( $tmpCategories );
		}
		else
		{
			$categoriesExport = $_POST['categories'];
		}

		// send headers
		header('Content-type: application/ms-excel');
		header("Content-Disposition: attachment; filename=categoies.xls");
		header("Pragma: no-cache");
		header("Expires: 0");

		if ( !empty( $categoriesExport ) )
		{
			$categories = '';
			foreach( $categoriesExport as $key => $id )
			{
				$sql = "SELECT `Key`, `Parent_ID`, `Path`, `Lock` FROM `". RL_DBPREFIX ."categories` WHERE `ID` = '{$id}'";
				$catInfo = $this -> getRow( $sql );

				if ( $catInfo['Parent_ID'] != 0 )
				{
					$parent_key = $this -> getOne('Key', "`ID`='{$catInfo['Parent_ID']}'", 'categories');
					$catInfo['parent'] = $lang['categories+name+'. $parent_key];
				}
				else
				{
					$catInfo['parent'] = "";
				}
				unset( $catInfo['Parent_ID'] );

				$catInfo['name'] = $lang['categories+name+'. $catInfo['Key']];
				$categories .= "{$catInfo['name']}\t{$catInfo['parent']}\t{$catInfo['Path']}\t{$catInfo['Lock']}\n";
			}

			echo $categories;
			unset($categories);
			exit;
		}
		else
		{
			echo $lang['importExportCategories_empty'];
			exit;
		}
	}
}